<template>
	<view class="container">
		
		<u-cell-group class="class-group" :border="false">
			<u-cell-item :arrow="false">
				<view class="class-list">
					<view class="class-list-item">
						<view class="class-list-item__avatar">
							<image class="avatar" :src="classData.course.image" mode="aspectFill"></image>
							<view class="label">预约</view>
							<view class="number" @click="dropSwitch()"><text>{{classData.appointment.length}}</text>/{{classData.course.people_count}} <u-icon class="arrow" :name="showStatus?'arrow-down':'arrow-up'" color="#D7D7D7" size="18"></u-icon></view>
						</view>
						<view class="class-list-item__content">
							<view class="title">{{classData.course.name}}</view>
							<view class="star">难易度：<u-rate :current="classData.course.degree" :disabled="true" inactive-icon="star-fill" inactive-color="#DFDFDF" active-color="#F5C822" size="24" gutter="6"></u-rate></view>
							<view class="info">教练：{{classData.teacher.name}}</view>
							<view class="info">教室：{{classData.classroom.name}}</view>
							<view class="info">时间：{{ classData.start_time }}-{{classData.end_time}}</view>
						</view>
						<button open-type="share" class="share-box"><image class="share" src="../../../static/icons/icon_share.png" mode="aspectFit"></image></button>
					</view>
					<view class="class-list-user" v-if="classData.appointment.length>0 && showStatus">
						<view class="title">已预约上课的学员</view>
						<view class="thumb-box">
							<image class="thumb" v-for="(img,idx) in classData.appointment" :key="idx" :src="img.avatar" mode="aspectFill"></image>
						</view>
					</view>
				</view>
			</u-cell-item>
			<u-cell-item :arrow="false">
				<view class="class-list">
					<view class="class-group__title">课程详情</view>
					<view class="class-group__text">
						<u-parse :html="classData.course.description"></u-parse>
					</view>
				</view>
			</u-cell-item>
		</u-cell-group>
		
		<u-cell-group class="class-group" :border="false" v-if="classData.my_appointment_count == 0">
			<u-cell-item title="会员卡" :arrow="true" @click="selectChange">
				<u-select v-model="selectShow" mode="single-column" :list="selectList" @confirm="selectConfirm"></u-select>
				<text class="class-group__title">{{ select.label }}</text>
			</u-cell-item>
			<u-cell-item title="预约人数" :arrow="false">
				<u-number-box class="wd-number-box" size="24" input-height="44" :min="1" v-model="people" @change="peopleChange"></u-number-box>
			</u-cell-item>
			<u-cell-item title="年卡有效期" :arrow="false" v-if="select.module_type==2">
				<text class="class-group__title">{{select.expire_in_text }}</text>
			</u-cell-item>
			<u-cell-item title="本次扣除次数" :arrow="false" v-if="select.module_type==1">
				<text class="class-group__title">{{classData.course.card_num}}次</text>
			</u-cell-item>
			<u-cell-item title="预约后卡内剩余次数" :arrow="false" v-if="select.module_type==1">
				<text class="class-group__title">{{select.value }}</text>
			</u-cell-item>
		</u-cell-group>
		
		<u-cell-group class="class-group" :border="false" v-if="classData.my_appointment_count == 0">
			
			<u-cell-item :arrow="false">
				<u-input type="textarea" placeholder="添加预约备注信息，非必填项" v-model="remarks" :clearable="false" />
			</u-cell-item>
		</u-cell-group>
		
		<view class="class-tips">
			<view class="class-tips__title">预约须知</view>
			<!-- <text class="class-tips__text"> -->
				<u-parse :html="classData.course.notice"></u-parse>
			<!-- </text> -->
		</view>
		
		<view class="wd-flex wd-buttonbar" v-if="classData.my_appointment_count == 0 && classData.appointment.length < classData.course.people_count">
			<view class="wd-flex-cell" >
				<u-button @click="appointment" :custom-style="wd_button_primary" shape="circle" :ripple="true">确定预约</u-button>
			</view>
		</view>
		<view class="wd-flex wd-buttonbar" v-else-if="classData.my_appointment_count == 0 && classData.appointment.length >=classData.course.people_count">
			<view class="wd-flex-left">
				<u-button @click="back()" :custom-style="wd_button_plain" shape="circle" :ripple="true" :plain="true">返回</u-button>
			</view>
			<view class="wd-flex-cell">
				<u-button @click="appointment" :custom-style="wd_button_primary" shape="circle" :ripple="true">人员已满，排队候补</u-button>
			</view>
		</view>
		<view class="wd-flex wd-buttonbar" v-else-if="classData.my_appointment_count == 1">
			<view class="wd-flex-cell" >
				<u-button :custom-style="wd_button_plain" shape="circle" :ripple="true">已预约</u-button>
			</view>
		</view>
	</view>
</template>

<script>
 	export default {
		data() {
			return {
				course_id:0,
				status: 1, // 0未预约，1未预约排队
				classData: {
					appointment:[]
				},
				showStatus:false,//预约头像显示
				select: { label: '尚未选择' },
				selectShow: false,
				selectList: [
					// { value: '1', label: '年卡', info: '2021.7.21' },
					// { value: '2', label: '次卡', info: '29次' },
				],
				people: 1,
				remarks: ''
			}
		},
		onReady() {
		
		},
		onLoad(options) {
			if(options.course_id){
				this.course_id = options.course_id;
				this.fetchDetail();
			}
		},
		onShareAppMessage() {
			let user = this.$core.getUserinfo();
			let path = '/pages/class/classDetail/classDetail?course_id=' + this.classData.id
			if(user){
				path += '&pid='+user.id;
			}
			return {
			    title: this.classData.course.name,
			    path: path,
			    imageUrl: this.classData.course.image || '',
			}
		},
		onShareTimeline() {
			let query = 'course_id='+this.classData.id
			let user = this.$core.getUserinfo();
			if(user){
				query += '&pid='+user.id;
			}
			return {
			    title: this.classData.course.name,
				query: query
			}
		},
		methods: {
			back(){
				uni.navigateBack({
					
				})
			},
			fetchDetail(){
				this.$core.get({url:'fitness.course_arrangement/detail',data:{course_arrangement_id:this.course_id},success:(ret)=>{
					this.classData = ret.data;
					let selectList = [];
					for(var i=0;i<ret.data.user_vip.length;i++){
						selectList[i] = { value: ret.data.user_vip[i].id, label: ret.data.user_vip[i].vip.name, info: '2021.7.21' }
					}
					this.selectList = selectList;
				}});
			},
			//显示预约人头像
			dropSwitch() {
			    this.showStatus = !this.showStatus
			},
			selectChange(e) {
				this.selectShow = !this.selectShow
			},
			selectConfirm(e) {
				this.selectList.map((val, index) => {
					if (e[0].value == val.value) {
						this.select = val;
						return ;
					}
				})
				console.log(this.select)
			},
			peopleChange(e) {
				console.log('当前值为: ' + e.value)
			},
			
			//预约
			appointment(){
				var that = this;
				if(!this.$core.getUserinfo(true)){
					return false;
				}
				var course_id = that.course_id;
				var vip = that.select;
				var appoint_num = that.people;
				var remarks = that.remarks;
				if(!vip.value) {
					uni.showToast({title: '您未选择抵扣会员卡',icon:'none'});
					return false;
				}
				that.$core.post({url:'fitness.course_arrangement/appointment',data:{course_arrangement_id:course_id,user_vip_id:vip.value,appoint_num:appoint_num,remarks:remarks},success:(ret)=>{
					uni.showToast({title:'预约成功',icon:'none'})
					that.fetchDetail();
					let ids = getApp().globalData.config.course_templates;
					uni.requestSubscribeMessage({
					  tmplIds: ids,
					  success (res) { 
						  
					  }
					})

					
				}});
			},
		},
	}
</script>

<style lang="scss" scoped>
	.class-group {
		display: block;
		margin-bottom: 16rpx;
		background-color: #FFFFFF;
		color: #333333;
		border-radius: 24rpx;
		overflow: hidden;
		
		.class-group__title,
		/deep/ .u-cell_title,
		/deep/ .u-input__textarea {
			font-size: 26rpx;
			color: #333333;
		}
		
		.class-group__text {
			margin-top: 10rpx;
			line-height: 34rpx;
			font-size: 24rpx;
			color: #D7D7D7;
		}
	}
	.class-list {
		padding: 0;
		text-align: left;
	}
	.class-tips {
		margin-bottom: 160rpx;
		padding: 26rpx 32rpx;
		line-height: 42rpx;
		
		.class-tips__title {
			font-size: 28rpx;
			color: #333333;
		}
		.class-tips__text {
			margin-top: 5rpx;
			font-size: 24rpx;
			color: #666666;
		}
	}
	.wd-buttonbar .wd-flex-left {
		margin-right: 24rpx;
		width: 280rpx;
	}
	.share-box {
		position: absolute;
		right: 25rpx;
		top: 200rpx;
		display: block;
		width: 40rpx;
		height: 40rpx;
		padding: 0;
		background: transparent;
		&::after {
			display: none;
		}
	}
	.share {
		display: block;
		width: 40rpx;
		height: 40rpx;
	}
</style>
